package com.aaa.service.impl;


import com.aaa.dao.UserDao;
import com.aaa.dao.impl.UserDaoImpl;
import com.aaa.entity.Card;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {

    private UserDao userDao =  new UserDaoImpl();



    @Override
    public boolean addUser(User user, double amount) {

        return false;
    }

    @Override
    public int addUser(User user, Card card) {
        //        获取最大的userId
        List<User> list = getMaxUserId();
        int userId = list.get(0).getUserId()+1;
//        获取最大的carId
        List<Card> list1 = getMaxCardId();
        int cardId = list1.get(0).getCardId() + 1;
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCreatedTime(simpleDateFormat.format(date));
        user.setUserId(userId);
        user.setCardId(cardId);
//        操作user，添加会员
        int num =  userDao.addUser(user);

        card.setCardId(cardId);
        card.setUserId(userId);

//       操作表card，添加会员卡
        int num2 = userDao.addCard(card);

        System.out.println(num + "    "+ num2);

        if (num < 1 || num2 < 1){
            return 0;
        }
        return 1;
    }

    @Override
    public int deleteByuserId(User user, Card card) {
        int i = userDao.deleteUserByuserId(user);
        int j = userDao.deleteCardByuserId(card);
        if (i< 1 || j< 1){
            return 0;
        }
        return 1;
    }

    @Override
    public int updateUserByUserId(User user) {
        return userDao.updateUserByUserId(user);
    }

    @Override
    public List<User> maxCardId() {
        return userDao.maxCardId();
    }

    //        获取最大的userId
    @Override
    public List<User> getMaxUserId() {
        return userDao.getMaxUserId();
    }

    //        获取最大的carId
    @Override
    public List<Card> getMaxCardId() {
        return userDao.getMaxCardId();
    }

    @Override
    public List<Map<String, Object>> getDataByNearYear() {
        return userDao.getDataByNearYear();
    }

    @Override
    public Map<String, Object> getAllUser(Integer pageNumber, Integer pageSize, String searchId, String searchName) {
        List<User> list = userDao.getAllUser(pageNumber, pageSize, searchId, searchName);
        int count = userDao.getAllUserInfoCount(searchId,searchName);
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("count",count);

        return map;
    }

    /**
     * 近一年用户增长趋势统计
     * @return
     */
	@Override
    public Map<String, Object> findUserCreatedTimeInfoForUserCount() {
        //返回给servlet
        Map<String, Object> dataMap = new HashMap<>();
        //存放年份
        int[] years = new int[2];
        //存放获取的月份
        int[] months = DateUtils.getMonthByNearYear();
        //测试数据{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
        //String数组，便于数据库查询
        String[] myMonths = new String[12];
        //月份加上'0'-> 01月
        for (int i = 0; i < months.length; i++) {
            if (months[i] < 10) {
                myMonths[i] = "0" + String.valueOf(months[i]);
            } else {
                myMonths[i] = String.valueOf(months[i]);
            }
        }

        //可能会分为两个年份
        //1.如果月份数组最开始 是1，则只有一年

        //2.如果月份数组最开始 不是1，则有两年
        //月份数组months最开始的月份-1往前到1月是最近的一年（”今年“）的月份
        //月份数组months最开始的月份往后到12月是上一年的月份
        //"今年"
        int year = DateUtils.getYears();

        //上一年
        if (months[0] != 1) {
            years[1] = year;
            int year0 = year - 1;
            years[0] = year0;
            //有两个月份（不同年份的月份数据分开）数据

            //上一年
            int[] lastYearValues = new int[12 - months[0]+1];
            //获取每月的数据
            for (int i = 0; i <= 12 - months[0]; i++) {
                //查询数据统计数据
                int count = userDao.findUserCreatedTimeInfoForUserCount(String.valueOf(year0)+"-"+myMonths[i]);
                lastYearValues[i] = count;
            }

            //”今年“
            int[] values = new int[12];

            //获取每月的数据
            for (int i = 12-months[0]+1; i < 12 ; i++) {
                //查询数据统计数据
                int count = userDao.findUserCreatedTimeInfoForUserCount(String.valueOf(year)+"-"+myMonths[i]);
                values[i] = count;
            }

            //返回数据
            dataMap.put("years", years);
            dataMap.put("months", months);
            dataMap.put("lastYearValues", lastYearValues);
            dataMap.put("values", values);
        } else {
            int[] values = new int[12];
            //只有一年的情况
            years[1] = year;
            //获取每月的数据
            for (int i = 0; i < myMonths.length; i++) {
                //查询数据统计数据
                int count = userDao.findUserCreatedTimeInfoForUserCount(String.valueOf(year)+"-"+myMonths[i]);
                values[i] = count;
            }
            //返回数据
            //删除
            years[0]=year;
            dataMap.put("years", years);
            dataMap.put("months", months);
            dataMap.put("values", values);
        }

        return dataMap;
    }

	/**
	*会员卡号查询会员信息
	*/
    @Override
    public List<String> findUserByCardid(Integer uid) {
        return userDao.findUserByCardid(uid);
    }
}
